﻿using System;
using System.Collections.Generic;
using System.Linq;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.ContentSection;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.Tag;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Models.Home
{
    public partial class SubSectionsViewModel
    {
        public int SubSectionId { get; set; }
        public string SelectedTagsCsv { get; set; }
        public List<int> CurrentlySelectedTagsIds { get; set; }
        
    }
    public partial class SubSectionsViewModel
    {
        public List<ITag> SelectedTags
        {
            get
            {
                if (_selectedTags == null)
                {
                    _selectedTags = new Lazy<List<ITag>>(() => _tagQueries.GetSelectedTags(CurrentlySelectedTagsIds).ToList());
                }
                return _selectedTags.Value;
            }
        }
        private Lazy<List<ITag>> _selectedTags;
        public List<Tuple<IArticle,IUser>> ArticlesForSubSection
        {
            get
            {
                if (_articlesForSubSection == null)
                {
                    _articlesForSubSection = new Lazy<List<Tuple<IArticle, IUser>>>(() =>
                        (from article in _articleQueries.GetArticlesForSubSectionsWithTagFilter(CurrentlySelectedTagsIds, SubSectionId, false).ToList()
                         let author = _userQueries.GetArticleAuthor(article.Id)
                         select new Tuple<IArticle, IUser>(article, author)).ToList());
                }
                return _articlesForSubSection.Value;
            }
        }
        private Lazy<List<Tuple<IArticle, IUser>>> _articlesForSubSection;
        public Tuple<IContentSection,ISubSection> SubSection
        {
            get
            {
                if (_subSection == null)
                {
                    _subSection = new Lazy<Tuple<IContentSection, ISubSection>>(() => _subSectionQueries.GetWithContentSection(SubSectionId));
                }
                return _subSection.Value;
            }
        }
        private Lazy<Tuple<IContentSection,ISubSection>> _subSection;
        public List<ITag> AllTags
        {
            get
            {
                if (_allTags == null)
                {
                    _allTags = new Lazy<List<ITag>>(() => _tagQueries.GetAll().ToList());
                }
                return _allTags.Value;
            }
        }
        private Lazy<List<ITag>> _allTags;
    }
    public partial class SubSectionsViewModel
    {
        private readonly IArticleQueries _articleQueries;
        private readonly ISubSectionQueries _subSectionQueries;
        private readonly ITagQueries _tagQueries;
        private readonly IUserQueries _userQueries;
        public SubSectionsViewModel(IArticleQueries articleQueries,
            ITagQueries tagQueries,
            ISubSectionQueries subSectionQueries,
            IUserQueries userQueries)
        {
            _articleQueries = articleQueries;
            _tagQueries =tagQueries;
            _subSectionQueries = subSectionQueries;
            _userQueries = userQueries;
        }
    }
}